#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <stdlib.h>
#include <vector>
#include <unordered_map>
#include <algorithm>

using namespace std;

struct node{
	string name;
	int cnt;
};

vector<node>ret;
unordered_map<string,string>father;
unordered_map<string,int>weight;
unordered_map<string,vector<string> >cluster;

string find_father(string a);

void merge(string a, string b){
	father[find_father(a)]=find_father(b);
}

string find_father(string a){
	return a==father[a]?a:(father[a]=find_father(father[a]));
}

bool cmp(node a,node b){
	return a.name<b.name;
}

int main(){
	int n,k;
	scanf("%d %d",&n,&k);
	string a,b;
	int time;
	for(int i=0;i<n;i++){
		cin>>a>>b>>time;
		weight[a]+=time;
		weight[b]+=time;
		if(father[a]==""){
			father[a]=a;
		} 
		if(father[b]==""){
			father[b]=b;
		}
		merge(a,b);
	}
	for(auto it=father.begin();it!=father.end();it++){
		cluster[find_father(it->second)].emplace_back(it->first);
	}
	for(auto it=cluster.begin();it!=cluster.end();it++){
		int cnt=0,max=-1,sum=0;
		string head;
		for(int i=0;i<it->second.size();i++){
			string tem=it->second[i];
			cnt++;
			sum+=weight[tem];
			if(weight[tem]>max){
				max=weight[tem];
				head=tem;
			}
		}
		if(cnt>2&&sum/2>k){
			node tem;
			tem.cnt=cnt;
			tem.name=head;
			ret.emplace_back(tem);
		}
	}
	sort(ret.begin(),ret.end(),cmp);
	printf("%d\n",ret.size());
	for(int i=0;i<ret.size();i++){
		cout<<ret[i].name<<" "<<ret[i].cnt<<endl;
	}
	return 0;
} 
